<script setup>
import {onMounted, ref} from 'vue'
import {useRoute} from 'vue-router'
import service from '@/api/request0.js'
import { useUserStore } from '@/stores/userStore';

const route = useRoute()
const items = ref([])
const userStore = useUserStore();


const fetchItems = async () => {
  try {
    items.value = await service.get(`/item/product/${route.params.productId}`)
  } catch (error) {
    console.error('获取商品项列表失败:', error)
  }
}

const goToAddItem = async (item) => {
  // 检查用户是否登录
  if (!userStore.user.userid) {
    console.error('请先登录');
    return;
  }
  try {
    const requestData = {
      userId: userStore.user.userid,
      itemId: item.itemId,
      productId: item.productId,
      listPrice: item.listPrice,
      quantity: 1,
      total: item.listPrice * 1
    };

    // 打印请求信息，方便调试
    console.log('用户ID:', userStore.user.userid);
    console.log('请求地址:', '/cartItem');
    console.log('请求数据:', requestData);

    const response = await service.post('/cartItem', requestData);
    console.log('接口响应:', response); 
    if (response && response.data.code === 0) {
      console.log('商品已成功添加到购物车');
    } else {
      console.error('添加商品到购物车失败:', response ? response.data.message : '未获取到有效响应');
    }
  } catch (error) {
    console.error('添加商品到购物车失败:', error);
  }
};
onMounted(fetchItems)
</script>

<template>
  <div class="item-list">
    <h2 class="list-title">商品项列表</h2>
    <div class="card-container">
      <div v-for="item in items" :key="item.itemId" class="item-card">

        <div class="info-group">
          <div class="info-item">
            <label>商品ID：</label>
            <span class="value">{{ item.itemId }}</span>
          </div>
          <div class="info-item">
            <label>产品ID：</label>
            <span>{{ item.productId }}</span>
          </div>
          <div class="info-item">
            <label>商品描述：</label>
            <span>{{ item.attribute1 }}</span>
          </div>
        </div>
        <div class="price-group">
          <div class="price-tag">
            ¥{{ item.listPrice }}
          </div>
        </div>
        <div class="action-group">
          <router-link
              :to="{ name: 'ItemDetail', params: { itemId: item.itemId } }"
              class="detail-button"
          >
            <i class="fas fa-info-circle"></i>
            查看详情
          </router-link>
          <button
              class="action-button"
              @click="() => goToAddItem(item)"
          >
            <i class="fas fa-cart-plus"></i>
            加入购物车
          </button>
        </div>
      </div>
    </div>
  </div>

</template>

<style scoped>
.item-list {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.list-title {
  color: #2c3e50;
  margin-bottom: 1.5rem;
  font-size: 1.8rem;
  text-align: center;
}

.card-container {
  display: grid;
  gap: 1.5rem;
}

.item-card {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s ease;
}

.item-card:hover {
  transform: translateY(-2px);
}

.item-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-decoration: none;
  color: inherit;
}

.info-group {
  flex: 1;
  margin-right: 1rem;
}

.info-item {
  display: flex;
  align-items: center;
  margin-bottom: 0.8rem;
  font-size: 0.95rem;
}

.info-item label {
  color: #666;
  min-width: 80px;
  margin-right: 0.5rem;
}

.price-group {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.price-tag {
  background: linear-gradient(135deg, #3b82f6, #6366f1);
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  font-weight: 600;
  font-size: 1.1rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.action-group {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  margin-top: 1rem;
}

.detail-button {
  padding-inline: 1.5rem;
  max-width: 200px;
  min-width: 120px;
}

.action-button {
  padding: 0.8rem 1.2rem;
  max-width: 200px;
  min-width: 120px;
}

@media (max-width: 640px) {
  .action-group {
    flex-direction: row;
    justify-content: center;
  }

  .detail-button,
  .action-button {
    max-width: 160px;
    min-width: 100px;
    padding: 0.6rem 1rem;
  }
}
</style>